package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;

import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;
import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils;
import cn.hamster3.mc.plugin.core.bukkit.util.MinecraftVersion;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class LoreInfoCommand extends ChildCommand {
    public static final LoreInfoCommand INSTANCE = new LoreInfoCommand();

    private LoreInfoCommand() {
    }

    @Override
    public @NotNull String getName() {
        return "info";
    }

    @Override
    public @NotNull String getUsage() {
        return "info";
    }

    @Override
    public boolean hasPermission(@NotNull CommandSender sender) {
        return true;
    }

    @Override
    public @NotNull String getDescription() {
        return "查看手持物品的信息";
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
        if (!(sender instanceof Player)) {
            CoreMessage.COMMAND_MUST_USED_BY_PLAYER.show(sender);
            return true;
        }
        Player player = (Player) sender;
        ItemStack stack = player.getItemInHand();
        if (CoreBukkitUtils.isEmptyItemStack(stack)) {
            CoreMessage.COMMAND_LORE_HAND_EMPTY.show(player);
            return true;
        }

        HamsterCorePlugin.getInstance().getAudienceProvider().sender(sender).sendMessage(
                Component.text("物品名称: ")
                        .color(NamedTextColor.GREEN)
                        .append(CoreBukkitUtils.getItemNameComponent(stack))
        );
        ItemMeta meta = stack.getItemMeta();
        if (meta == null) {
            sender.sendMessage("§a物品自定义模型 ID: 无");
            sender.sendMessage("§a物品 lore: 无");
            return true;
        }
        if (MinecraftVersion.getVersion2() >= 13) {
            if (meta.hasCustomModelData()) {
                sender.sendMessage("§a物品自定义模型 ID: " + meta.getCustomModelData());
            } else {
                sender.sendMessage("§a物品自定义模型 ID: 无");
            }
        }
        List<String> lore = meta.getLore();
        if (lore != null) {
            sender.sendMessage("§a物品 lore: ");
            for (int i = 0; i < lore.size(); i++) {
                String s = lore.get(i);
                sender.sendMessage("  §a" + (i + 1) + ". §f" + s);
            }
        } else {
            sender.sendMessage("§a物品 lore: 无");
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
        return null;
    }
}
